<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Code Editor</title>
  <style type="text/css" media="screen">
    body {
        overflow: hidden;
    }
    #editor {
        margin: 0 0 0 0;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
		border:1px #ebebeb solid;
    }
	body{
		background:#eee;
	}
	.button {
		background-color: white; /* Green */
		border: none;
		color: black;
		padding: 5px 20px;
		text-align: center;
		text-decoration: none;
		display: inline-block;
		font-size: 13px;
		margin: 0px 0px;
		cursor: pointer;
		border: 1px solid #fff;
		border-radius: 2px;
	}
	.button:hover {
		color: #108ee9; 
		border: 1px solid #108ee9;
	}

	#loader-container {
	  width: 60px;
	  height: 60px;
	  color: #3498db;
	  margin: 0 auto;
	  position: absolute;
	  top: 50%;
	  left: 50%;
	  z-index:1000;
	  margin-right: -50%;
	  transform: translate(-50%, -50%);
	  border: 2px solid #3498db;
	  border-radius: 50%;
	  -webkit-animation: borderScale 1s infinite ease-in-out;
	  animation: borderScale 1s infinite ease-in-out;
	}

	#loadingText {
	  font-family: 'Raleway', sans-serif;
	  font-weight: bold;
	  font-size: 12px;
	  position: absolute;
	  top: 32%;
	  left: 50%;
	  margin-right: -50%;
	  transform: translate(-50%, -50%);
	}

	@-webkit-keyframes borderScale {
	  0% {
		border: 5px solid white;
	  }
	  50% {
		border: 25px solid #3498db;
	  }
	  100% {
		border: 5px solid white;
	  }
	}

	@keyframes borderScale {
	  0% {
		border: 2px solid white;
	  }
	  50% {
		border: 8px solid #3498db;
	  }
	  100% {
		border: 2px solid white;
	  }
	}

  </style>
</head>
<body>
	<div id="toolbar" style="display:none">
		<button class="button" onclick="saveCode()" >保存(Ctr+S)</button> 
		<button class="button" onclick="saveCompile()" >保存并编译</button> 
		<button class="button" onclick="readFileCode()">读取Java文件源码</button>
	</div>
	<pre id="editor" ></pre>
	<div id="loader-container" style="display:none">
	  <p id="loadingText">Waiting</p>
	</div>
<script src="src-min-noconflict/ace.js" type="text/javascript" ></script>
<script src="src-min-noconflict/ext-language_tools.js"></script>
<script src="jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script>
    var editor = ace.edit("editor");
	var beanId=GetUrlArg("beanId");
	var appId=GetUrlArg("appId");
	var token=getCookie("identitytoken");
	var codeType=GetUrlArg("codeType");
	if(codeType==''||codeType==undefined){codeType="java";}
	editor.setOptions({
            enableBasicAutocompletion:false,
            enableSnippets:true,
            enableLiveAutocompletion:true
    });
	editor.$blockScrolling = Infinity;
    editor.setTheme("ace/theme/eclipse");
    editor.session.setMode("ace/mode/"+codeType);
	editor.setShowPrintMargin(false);
	editor.renderer.setShowPrintMargin(false);
	editor.commands.addCommands([{
		name: "savedoc",
		bindKey: {win: "Ctrl-s", mac: "Command-s"},
		exec: function(editor) {saveCode();},
		readOnly: true
	}]);
	editor.on("change", function(e){editor.execCommand("startAutocomplete");})
	initCode();

	//初始化代码
	function initCode(){
		var url=parent.host+"/rest/core/coderepository/detail/beanid?beanId="+beanId;
		$.ajax({
			beforeSend: function(request) {
			  request.setRequestHeader("identitytoken", token); //在http头中增加token头
			},
			url:url,
			type: "get",
			success: function (data) {
			  if(data.code){
				editor.insert(data.code);
			  }else{
				editor.insert("No code found in the code repository...");
			  }
			}
		});
	}

	//设置代码
	function setCode(code){
		editor.setValue("");
		editor.insert(code);
	}

	//从java源文件中读取代码
	function readFileCode(){
		var url=parent.host+"/rest/core/coderepository/srcfile/readcode?beanId="+beanId;
		var msg="";
		loading();
		$.ajax({
			beforeSend: function(request) {
			  request.setRequestHeader("identitytoken", token); //在http头中增加token头
			},
			url:url,
			type: "get",
			async:false,
			success: function (data) {
  			  unloading();
			  if(data.state==false){
				msg=data.msg;
			  }else{
			    editor.setValue("");
				editor.insert(data.code);
			  }
			}
		});
		return msg;
	}

	//保存代码
	function saveCode(){
		var url=parent.host+"/rest/core/coderepository/save";
		var code=editor.getValue();
		var msg="";
		loading();
		$.ajax({
			beforeSend: function(request) {
			  request.setRequestHeader("identitytoken", token); //在http头中增加token头
			},
			url:url,
			type: "post",
			async:false,
			data:{beanId:beanId,appId:appId,code:code},
			success: function (data) {
			  unloading();
			  if(data.state==false){
				msg=data.msg;
			  }
			}
		});
		return msg;
	}

	//覆盖代码
	function overFileCode(){
		var url=parent.host+"/rest/core/coderepository/srcfile/overcode";
		var code=editor.getValue();
		var jsonData={};
		loading();
		$.ajax({
			beforeSend: function(request) {
			  request.setRequestHeader("identitytoken", token); //在http头中增加token头
			},
			url:url,
			type: "post",
			async:false,
			data:{beanId:beanId,code:code},
			success: function (data) {
			  unloading();
			  jsonData=data;
			}
		});
		return jsonData;
	}

	//保存并编译
	function saveCompile(){
		var msg="";
		var url=parent.host+"/rest/core/coderepository/compile";
		var code=editor.getValue();
		loading();
		$.ajax({
			beforeSend: function(request) {
			  request.setRequestHeader("identitytoken", token); //在http头中增加token头
			},
			url:url,
			async:false,
			type: "post",
			data:{beanId:beanId,appId:appId,code:code},
			success: function (data) {
			  unloading();
			  if(data.state==false){
				msg=decodeURIComponent(data.msg);
			  }
			}
		});
		return msg;
	}
	
	function loading(){
		$("#loader-container").css("display","");
	}
	function unloading(){
		setTimeout(function () {$("#loader-container").css("display","none");}, 1000);
	}

	function getCookie(name){
	  let arr;
	  let reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	  if(arr=document.cookie.match(reg)){
		return unescape(arr[2]);
	  }else{
		return "";
	  }
	}

	function GetUrlArg(name){var reg = new RegExp("(^|\\?|&)"+ name +"=([^&]*)(\\s|&|$)", "i");       if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, " ")); return ""; };

</script>

</body>
</html>
